package cn.witsky.cdrService.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.witsky.cdrService.config.CdrConfig;
import cn.witsky.cdrService.constant.ValType;
import cn.witsky.cdrService.domain.CallFile;
import cn.witsky.cdrService.domain.base.Value;
import cn.witsky.cdrService.domain.callEvent.MoCall;
import cn.witsky.cdrService.request.MoCallRequest;

public class CallFileUtil {
	private static Logger logger = LoggerFactory.getLogger(CallFileUtil.class);
	public static int creatMoCallFile(LocalDateTime now) {
		String nowStr=CdrConfig.dtf.format(now);
		CallFile moCallfile =null;
		List<String> cdrArr = RedisUtil.getCdrList();
		if(cdrArr==null||cdrArr.size()==0) {
			return 0;
		}else{
			moCallfile=new CallFile(true);
			Value val = new Value();
			val.setType(ValType.TIME);
			val.setValStr(nowStr);
			moCallfile.getHeaderRecord().getProductionDateTime().setValBytes(val);
			val.setType(ValType.DecToHex);
			val.setValStr(cdrArr.size()+"");
			moCallfile.getHeaderRecord().getRecordingEntity().setValBytes(val);
			val.setType(ValType.NONE);
			val.setValStr(null);
			moCallfile.getHeaderRecord().getExtensions().setValBytes(val);
			
			for (String cdrStr : cdrArr) {
				JSONObject cdrJson = JSON.parseObject(cdrStr);
				MoCallRequest mtCallRequest = JSON.parseObject(cdrJson.toJSONString(), MoCallRequest.class);
				mtCallRequest.setDefaultVal();
				MoCall moCall=mtCallRequest.covertMoCall();
				moCallfile.getCallEvent().addMoCall(moCall);
			}
			val.setType(ValType.TIME);
			val.setValStr(nowStr);
			moCallfile.getTrailer().getProductionDateTime().setValBytes(val);

			val.setType(ValType.NONE);
			val.setValStr("00");
			moCallfile.getTrailer().getRecordingEntity().setValBytes(val);

			val.setType(ValType.TIME);
			val.setValStr(nowStr);
			moCallfile.getTrailer().getFirstCallDateTime().setValBytes(val);

			val.setType(ValType.TIME);
			val.setValStr(nowStr);
			moCallfile.getTrailer().getLastCallDateTime().setValBytes(val);

			val.setType(ValType.NONE);
			val.setValStr("00");
			moCallfile.getTrailer().getNoOfRecords().setValBytes(val);

			val.setType(ValType.NONE);
			val.setValStr(null);
			moCallfile.getTrailer().getExtensions().setValBytes(val);

			val.setType(ValType.NONE);
			val.setValStr(null);
			moCallfile.getExtensions().setValBytes(val);
			moCallfile.getLen(moCallfile);
		}
		boolean isSuc =false;
		File datFile =null;
		if(moCallfile!=null) {
			DataOutputStream d =null;				
			try {
				datFile= new File(CdrConfig.moCallFileName); 
				datFile.createNewFile();
				d=new DataOutputStream(new FileOutputStream(datFile));
				moCallfile.writeToFile(moCallfile, d);	
				isSuc=true;
			} catch (IOException e) {
				logger.error("生成单数据出现异常:",e);
			}finally {
				if(d!=null) {
					try {
						d.close();
					} catch (IOException e) {
						logger.error("生成单数据出现异常:",e);
					}
				}
			}
		}
		if(isSuc) {			
			return cdrArr.size();
		}else {
			return -1;
		}
	}
}
